// CEditCtrlFormat.cpp : Implementierungsdatei
//

#include "stdafx.h"
#include "Charplan.h"
#include "CEditCtrlFormat.h"


// CEditCtrlFormat

IMPLEMENT_DYNAMIC(CEditCtrlFormat, CEdit)
CEditCtrlFormat::CEditCtrlFormat()
{
}

CEditCtrlFormat::~CEditCtrlFormat()
{
}


BEGIN_MESSAGE_MAP(CEditCtrlFormat, CEdit)
	ON_WM_PAINT()
END_MESSAGE_MAP()



// CEditCtrlFormat-Meldungshandler


void CEditCtrlFormat::OnPaint()
{
	// basic settings
	CPaintDC dc(this); 

	CRect rect;
	GetClientRect(&rect);
	rect.DeflateRect(1, 1);

    dc.FillSolidRect(rect,GetSysColor(COLOR_BTNFACE));

	dc.SetBkMode(TRANSPARENT); 

	CString text;
	GetWindowText(text);

	// Copies default logfont's structure
	LOGFONT lf, logfont_base;
	GetFont()->GetLogFont(&logfont_base);
	memcpy(&lf, &logfont_base, sizeof(LOGFONT));

	CFont font;
	VERIFY(font.CreateFontIndirect(&lf));
	CFont* old_font = dc.SelectObject(&font);


	// init
	CPoint pos;
	pos = rect.TopLeft();

	enum{	ALIGN_LEFT = 0,
			ALIGN_CENTER,
			ALIGN_RIGHT
		} e_aligment = ALIGN_CENTER;


	CString line;
	CSize	lastsize(0,0);

	// run over it
	TCHAR* pc_run = text.GetBuffer();
	while (*pc_run || !line.IsEmpty())
	{
		// format
		if (*pc_run==_T('<')||*pc_run==_T('\n')||*pc_run==0)
		{
			if (!line.IsEmpty())
			{
				bool b_newline = false;
                lastsize = dc.GetTextExtent(line);
				while (pos.x+lastsize.cx > rect.Width())
				{
					b_newline = true;

					int p=line.ReverseFind(_T(' '));
					if (p==-1) p= line.GetLength()-1;
					pc_run -= (line.GetLength()-p);
					line.Delete(p,line.GetLength());

					lastsize = dc.GetTextExtent(line);
				}
				if (*pc_run==_T(' ')) pc_run++;

				CRect r(pos,pos);
				switch (e_aligment)
				{
					case ALIGN_LEFT:	break;
					case ALIGN_CENTER:	
								r.left = pos.x + (rect.Width()-pos.x  - lastsize.cx)/2;
								r.right = r.left;
								break;
					case ALIGN_RIGHT:
								r.left = rect.Width() - lastsize.cx;
								break;

				}

				r.bottom += min(lastsize.cy,rect.Height()-pos.y);
				r.right += min(lastsize.cx,rect.Width()-pos.x);
				dc.DrawText(line,&r,0);

				pos.x += lastsize.cx;
				line.Empty();

				if (b_newline)
				{
					pos.x = rect.left;
					pos.y += lastsize.cy;
					continue;
				}
			}

			// newline
			if (*pc_run==_T('\n'))
			{
				pos.x = rect.left;
				pos.y += lastsize.cy;
			}

			if (!_tcsnicmp(pc_run,_T("<br>"),4))
			{
				pos.x = rect.left;
				pos.y += lastsize.cy;
				pc_run+=3;
			}
		
			// align
			if (!_tcsnicmp(pc_run,_T("<al>"),4))		{ e_aligment = ALIGN_LEFT; pc_run+=3;}
			if (!_tcsnicmp(pc_run,_T("<al_l>"),6))	{ e_aligment = ALIGN_LEFT; pc_run+=5;}
			if (!_tcsnicmp(pc_run,_T("<al_c>"),6))	{ e_aligment = ALIGN_CENTER; pc_run+=5;}
			if (!_tcsnicmp(pc_run,_T("<al_r>"),6))	{ e_aligment = ALIGN_RIGHT; pc_run+=5;}

			// font
			if (!_tcsnicmp(pc_run,_T("<b>"),3))
			{
					dc.SelectObject(old_font);
					font.DeleteObject();
					lf.lfWeight = logfont_base.lfWeight;
					lf.lfWeight *= 2;
					if (lf.lfWeight > FW_BLACK)		lf.lfWeight = FW_BLACK;
					VERIFY(font.CreateFontIndirect(&lf));
					old_font = dc.SelectObject(&font);
					pc_run+=2;
			}
			if (!_tcsnicmp(pc_run,_T("</b>"),4))
			{
					dc.SelectObject(old_font);
					font.DeleteObject();
					lf.lfWeight = logfont_base.lfWeight;
					VERIFY(font.CreateFontIndirect(&lf));
					old_font = dc.SelectObject(&font);
					pc_run+=3;
			}
			if (!_tcsnicmp(pc_run,_T("<i>"),3))
			{
					dc.SelectObject(old_font);
					font.DeleteObject();
					lf.lfItalic = true;
					VERIFY(font.CreateFontIndirect(&lf));
					old_font = dc.SelectObject(&font);
					pc_run+=2;
			}
			if (!_tcsnicmp(pc_run,_T("</i>"),4))
			{
					dc.SelectObject(old_font);
					font.DeleteObject();
					lf.lfItalic = false;
					VERIFY(font.CreateFontIndirect(&lf));
					old_font = dc.SelectObject(&font);
					pc_run+=3;
			}
			if (!_tcsnicmp(pc_run,_T("<hr="),4))
			{
				while (*pc_run && *pc_run!='>') pc_run++;
				if (!*pc_run) pc_run--;

				CPen pen(PS_SOLID, 1, dc.GetTextColor());
				CPen *penOld = dc.SelectObject(&pen);
				dc.MoveTo(rect.left+2, pos.y+3);
				dc.LineTo(rect.right-2, pos.y+3);
				dc.SelectObject(penOld);
				pen.DeleteObject();
			}


		}
		else	line+= *pc_run;

		if (*pc_run) pc_run++;
	}


	dc.SelectObject(old_font);
	font.DeleteObject();

}
